package com.codeape.number.util;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.PopupWindow;

/**
 * sss
 *
 * @author LiuPeiming
 * Created at 2021/9/9 18:14
 */
public class CommonUtils {

    /**
     * @param pw     popupWindow
     * @param anchor v
     * @param xoff   x轴偏移
     * @param yoff   y轴偏移
     */
    public static void showAsDropDown(final PopupWindow pw, final View anchor, final int xoff, final int yoff) {
        closeKeyboard(anchor);
        if (Build.VERSION.SDK_INT >= 24) {
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
        }
        pw.showAsDropDown(anchor, xoff, yoff);
    }

    //如果显示则隐藏，如果隐藏则显示
    private static void closeKeyboard(View anchor) {
        InputMethodManager imm = (InputMethodManager) anchor.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        // 得到InputMethodManager的实例
        if (imm.isActive()) {
            // 如果开启
            imm.hideSoftInputFromWindow(anchor.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

}